<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use EasyWeChat;

class CalculateSellCart extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book-shop:calculate-sell-cart';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检测用户卖书不回收图书列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("开始获取...");

        $officialAccount = EasyWeChat::officialAccount(); // 公众号

        $list = DB::table('sell_carts')->where('type',0)->get();
        if($list->isNotEmpty())
        {
            foreach ($list as $v)
            {
                $book = DB::table('books')->where('id',$v->book_id)->first();
                if(isset($book->is_recycle) && $book->is_recycle == 1)
                {
                    DB::table('sell_carts')->where('id',$v->id)->update(['type' => 1, 'recyle_price'=>$book->recyle_price]);


                    $user = DB::table('users')->select(['id','unionid','wechat_nickname'])->where('id',$v->user_id)->first();
                    if(isset($user->unionid))
                    {
                        $wechat_user = DB::table('wechat_users')->where('unionid',$user->unionid)->first();
                        if(isset($wechat_user->openid) && !empty($wechat_user))
                        {
                            $openid = $wechat_user->openid;

                            $nickname = $user->wechat_nickname;
                            $book_title = $book->title;

                            $first = "{$nickname}，您之前扫码卖书不回收的《{$book_title}》，现在开始回收了\r\n";
                            $officialAccount->template_message->send([
                                'touser'        => $openid,
                                'template_id'   => 'XPrCJKucm-v6XBqZTQ6PpoSTV1nBitVIZgMhCmxliM0',
                                'url'  => '',
                                'miniprogram' => [
                                    'appid'     => config('wechat.mini_program.default.app_id'),
                                    'path'  => '/pages/sale/index/index',
                                ],
                                'data' => [
                                    'first'     => $first,
                                    'keyword1'  => '1本',
                                    'keyword2'  => '开始支持回收',
                                    'keyword3'  => date('Y-m-d')."\r\n",
                                    'remark'    => '点击详情去卖书列表查看'
                                ],
                            ]);

                        }
                    }
                }
            }
        }

        $this->info("获取成功...");
    }
}
